package com.houtai.controller.resume;

import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.resume.TdResumeInsertDto;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.file.FileVo;
import com.houtai.bean.vo.resume.TdResumeDetailVo;
import com.houtai.common.Contant;
import com.houtai.common.oss.OssUtil;
import com.houtai.common.util.CommonUtil;
import com.houtai.service.dict.DictService;
import com.houtai.service.resume.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Api(value="简历接口", tags={"简历接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class ResumeContorller {
    @Autowired
    private DictService dictService;

    @Autowired
    private ResumeService resumeService;

    @ApiOperation(value="添加简历", notes="添加简历")
    @Authority(AuthorityType.UserReal)
    @RequestMapping(value = "/resume/insert")
    public ResultVo insert(@ApiParam(value = "添加简历所需参数") @Valid @RequestBody TdResumeInsertDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = resumeService.insertResume(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="编辑简历", notes="编辑简历")
    @Authority(AuthorityType.UserReal)
    @RequestMapping(value = "/resume/update")
    public ResultVo update(@ApiParam(value = "编辑简历所需参数") @Valid @RequestBody TdResumeInsertDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = resumeService.updateResume(dto, token);
        }
        return retInfo;
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="简历详情", notes="简历详情")
    @RequestMapping(value = "/resume/detail", method = RequestMethod.POST)
    public TdResumeDetailVo detail(@RequestHeader String token) {
        return resumeService.resumeDetail(token);
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="当前求职状态列表", notes="当前求职状态列表")
    @RequestMapping(value = "/resume/job/status", method = RequestMethod.POST)
    public DictListResult status() {
        return dictService.findDictByType("job_status");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="学历列表", notes="学历列表")
    @RequestMapping(value = "/resume/diploma", method = RequestMethod.POST)
    public DictListResult diploma() {
        return dictService.findDictByType("diploma");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="职位列表", notes="职位列表")
    @RequestMapping(value = "/resume/jobs", method = RequestMethod.POST)
    public DictListResult jobs() {
        return dictService.findDictByType("jobs");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="工资区间列表", notes="工资区间列表")
    @RequestMapping(value = "/resume/wage", method = RequestMethod.POST)
    public DictListResult wage() {
        return dictService.findDictByType("wage");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="所属行业列表", notes="所属行业列表")
    @RequestMapping(value = "/resume/industry", method = RequestMethod.POST)
    public DictListResult industry() {
        return dictService.findDictByType("industry");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="技能标签列表", notes="技能标签列表")
    @RequestMapping(value = "/resume/skills/label", method = RequestMethod.POST)
    public DictListResult skills_label() {
        return dictService.findDictByType("skills_label");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="项目角色列表", notes="项目角色列表")
    @RequestMapping(value = "/resume/project/roles", method = RequestMethod.POST)
    public DictListResult project() {
        return dictService.findDictByType("project_roles");
    }

    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="所属部门列表", notes="所属部门列表")
    @RequestMapping(value = "/resume/department", method = RequestMethod.POST)
    public DictListResult department() {
        return dictService.findDictByType("department");
    }

    @Authority(value = AuthorityType.UserReal)
    @ApiOperation(value="简历照片上传文件(次接口为表单类型)", notes="作品上传文件")
    @RequestMapping(value = "/resume/file", method = RequestMethod.POST)
    public FileVo file(@RequestParam(value = "frontFile", required = true) MultipartFile frontFile)throws Exception{
        FileVo vo = new FileVo();
        if (CommonUtil.checkFileSize(frontFile.getSize(), 100,"K")) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssS", Locale.CHINA);

            String frontFileName = frontFile.getOriginalFilename().substring(frontFile.getOriginalFilename().lastIndexOf(".") + 1);
            String imgName = formatter.format(new Date(System.currentTimeMillis()));
            String frontSavePath = Contant.FILE_IMG+"/" + "resume/" + imgName + "." + frontFileName;
            ObjectMetadata frontMetadata = new ObjectMetadata();

            InputStream inputStream = frontFile.getInputStream();
            frontMetadata.setContentLength(inputStream.available());
            OssUtil.uploadInputStream(inputStream, frontMetadata, frontSavePath);
            vo.setMark("0");
            vo.setTip("查询成功");
            vo.setFile_name(imgName + "." + frontFileName);
            vo.setFile_url(Contant.DOMAIN +  Contant.FILE_IMG+"/"+"resume"+"/"+imgName + "." + frontFileName);
        } else {
            vo.setMark("1");
            vo.setTip("照片最大100Kb");
        }

        return vo;
    }
    @Authority(value = AuthorityType.UserReal)
    @ApiOperation(value="附件简历上传(次接口为表单类型)", notes="附件简历上传")
    @RequestMapping(value = "/resume/attachment", method = RequestMethod.POST)
    public FileVo attachment(@RequestParam(value = "frontFile", required = true) MultipartFile frontFile,
                             @RequestHeader String token)throws Exception{
        FileVo vo = new FileVo();
        if (CommonUtil.checkFileSize(frontFile.getSize(), 10,"M")) {
            vo = resumeService.updateTdResumeAttachmentUrl(token, frontFile);
        } else {
            vo.setMark("1");
            vo.setTip("附件简历最大10M");
        }
        return vo;

    }
}
